library(nycflights13)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
flights
If we want to see all the data in flights: View(flights)
allows you to subset observations based on their values. The first argument is the name of the dataframe. The second and the rest arguments are the expressions that filter the dataframe. For example, we can select all flights on January 1st with:
filter(flights, month == 1, day ==1)
To assign the filtered result into a new dataframe we can use an assignement:
jan1 <- filter(flights, month == 1, day == 1)
If you want to print the results and save them in a variable at the same time, you can use parentheses:
(dec25 <- filter(flights, month ==12, day == 25))
The standard operations that R uses are: >, >=, <, <=, != (not equal), and == (equal).
sqrt(2) ^ 2 == 2
## [1] FALSE
1/49 *49 == 1
## [1] FALSE
near(sqrt(2) ^ 2, 2)
## [1] TRUE
near(1 / 49 * 49, 1)
## [1] TRUE
Boolean operators in R are: & is "and"", | is "or"", ! is "not". The following code shows the months of November or December:
filter(flights, month == 11 | month == 12)
nov_dec <- filter(flights, month %in% c(11, 12))
filter(flights, !(arr_delay > 120 | dep_delay > 120))
filter(flights, arr_delay <= 120, dep_delay <= 120)
if you want to determine if a value is missing, use it.na():
x <- NA
is.na(x)
## [1] TRUE
df <- tibble(x = c(1, NA, 3))
filter(df, x > 1)
filter(df, is.na(x) | x > 1)
Finding all flights that flew to Houston:
flights
filter(flights, dest == "IAH" | dest == "HOU")
View(flights)
Finding all flights that were operated by United, American, or Delta:
filter(flights, carrier == "UA" | carrier == "AA" | carrier == "DL")
filter(flights, between(x = month, left = 7, right = 9))